package com.ubaer.zsycz.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lsw.Base.AdapterRefresh;
import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.inject.Constants;
import com.ubaer.zsycz.model.OrderModel;
import com.ubaer.zsycz.ui.adapter.SearchResultAdapter;
import com.ubaer.zsycz.ui.base.activity.BaseActivity;
import com.ubaer.zsycz.ui.dialog.RechargeProcessingDialog;
import com.ubaer.zsycz.ui.dialog.SuccessDialog;
import com.ubaer.zsycz.ui.fragment.UntreatedContract;
import com.ubaer.zsycz.ui.fragment.UntreatedPresenter;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class SearchResultActivity extends BaseActivity<UntreatedPresenter> implements PullToRefreshLayout.OnRefreshListener, AdapterRefresh, UntreatedContract.View {

    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    //搜索结果
    @BindView(R.id.listview)
    PullableListView listview;
    @BindView(R.id.iv_return)
    ImageView iv_return;
    @BindView(R.id.tv_seach)
    TextView tv_seach;
    //关键字
    String seach;
    SearchResultAdapter searchResultAdapter;
    RechargeProcessingDialog rechargeProcessingDialog;
    //订单状态  0待处理 1充值成功 2充值失败
    String status = "";
    int page = Constants.PAGE_START;
    //处理状态
    SuccessDialog successDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        successDialog = new SuccessDialog(mContext);
        seach = getIntent().getStringExtra("seach");
        if (!StringUtil.isNullOrEmpty(seach))
            tv_seach.setText(seach);
        pullToRefreshLayout.setOnRefreshListener(this);
        listview.setCanPulldown(true);
        listview.setCanPullup(true);
        rechargeProcessingDialog = new RechargeProcessingDialog(mContext);
        iv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        searchResultAdapter = new SearchResultAdapter(mContext);
        searchResultAdapter.setAdapterRefresh(this);
        listview.setAdapter(searchResultAdapter);
        listview.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(mContext, OrderDetailsActivity.class);
            intent.putExtra("id", searchResultAdapter.getList().get(position).getId());
            startActivity(intent);
        });
        mPresenter.getOrderList(status, seach, page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    @Override
    public void setOrderList(List<OrderModel> list, boolean ispage) {
        if (list == null)
            return;
        listview.setCanPullup(ispage);
        if (page == Constants.PAGE_START) {
            searchResultAdapter.setData(list);
        } else {
            searchResultAdapter.addData(list);
        }
        if (searchResultAdapter.getList().size() == 0) {
            pullToRefreshLayout.setNoData(View.VISIBLE);
        } else {
            pullToRefreshLayout.setNoData(View.INVISIBLE);
        }
    }

    int pos;
    String statu;

    @Override
    public void setProcessingSucceed() {
        //处理成功
        searchResultAdapter.getList().get(pos).setStatus(Integer.valueOf(statu));
        searchResultAdapter.notifyDataSetChanged();
        if (searchResultAdapter.getList().size() == 0) {
            pullToRefreshLayout.setNoData(View.VISIBLE);
        } else {
            pullToRefreshLayout.setNoData(View.INVISIBLE);
        }
        successDialog.show(1, "");
    }

    @Override
    public void setProcessingSucceeds() {

    }

    @Override
    public void getRefresh() {
        onRefresh(null);
    }

    @Override
    public void setProcessingfail(String msg) {
        successDialog.show(2, msg);
    }

    @Override
    public void RefreshSucceed() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void RefreshFail() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.FAIL);
    }

    @Override
    public void openLogin() {
        OpenLogin();
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {
        if (taskid == 1) {
            rechargeProcessingDialog.show(searchResultAdapter.getList().get(position).getOrder_no(), new RechargeProcessingDialog.ProcessingResult() {
                @Override
                public void Result(String status, String reason) {
                    rechargeProcessingDialog.dismiss();
                    pos = position;
                    statu = status;
                    mPresenter.ProcessingOrder(searchResultAdapter.getList().get(position).getId(), status, reason);
                }
            });
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = Constants.PAGE_START;
        mPresenter.getOrderList(status, seach, page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.getOrderList(status, seach, ++page, Constants.PAGE_DEFAULT_COUNT + "");
    }

}
